/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.service.catalogue.common;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;

/**
 *
 * @author khaleddaham
 */
public class RestClientHelper {

    static Client client;
    static WebTarget root;

    /**
     *
     * @param URL
     * @param json
     * @return
     */
    public static void sendJSON(String URL, String json) throws Exception {
        ClientRequest request = new ClientRequest(URL);
        request.accept("application/json; charset=UTF-8");
        request.body("application/json; charset=UTF-8", json);
        ClientResponse<String> response = request.post(String.class);

        if (response.getStatus() != 204) {
            System.out.println("Failed with HTTP error code: " + response.getStatus());
        }
    }

    /**
     *
     * @param URL
     * @param json
     * @throws Exception
     */
    public static void getJSON(String URL, String json) throws Exception {
        ClientRequest request = new ClientRequest(URL);
        request.accept("application/json; charset=UTF-8");
        request.body("application/json; charset=UTF-8", json);
        ClientResponse<String> response = request.post(String.class);
        if (response.getStatus() != 204) {
            System.out.println("Failed with HTTP error code: " + response.getStatus());
        }
    }

    /**
     *
     * @param URL
     * @param request
     */
    public static boolean sendJSONObject(String URL, Object request, int okStatus) throws Exception {
        ClientRequest client = new ClientRequest(URL);
        client.accept("application/json; charset=UTF-8");
        client.body("application/json; charset=UTF-8", request);
        ClientResponse<Object> response = client.post(Object.class);
        if (response.getStatus() != okStatus) {
            return false;
        }
        return true;
    }

    public static boolean putJSONObject(String URL, Object request, int okStatus) throws Exception {
        ClientRequest client = new ClientRequest(URL);
        client.accept("application/json; charset=UTF-8");
        client.body("application/json; charset=UTF-8", request);
        ClientResponse<Object> response = client.put(Object.class);
        System.out.println("url: " + URL + ", code: {}" + response.getStatus());
        if (response.getStatus() != okStatus) {
            return false;
        }
        return true;
    }

    public static void postQueryParam(String URL, String paramName, String paramValue, int okStatus) throws Exception {
        ClientRequest client = new ClientRequest(URL);
        client.accept("application/json; charset=UTF-8");
        client.queryParameter(paramName, paramValue);
        ClientResponse<Object> response = client.post(Object.class);
        if (response.getStatus() != okStatus) {
            System.out.println("Failed with HTTP error code: " + response.getStatus());
        }
    }

    public static Response sendJSONObject(Object request, String restURL) {
        ClientConnectionManager cm = new ThreadSafeClientConnManager();
        HttpClient httpClient = new DefaultHttpClient(cm);
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        final String mediaType = "application/json; charset=UTF-8";
        final Entity<Object> entity = Entity.entity(request, mediaType);
        Response r = client.target(restURL).request().post(entity, Response.class);
        client.close();
        return null;
    }

    public static Response putJSONObject(Object request, String restURL) {
        ClientConnectionManager cm = new ThreadSafeClientConnManager();
        HttpClient httpClient = new DefaultHttpClient(cm);
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        final String mediaType = "application/json; charset=UTF-8";
        final Entity<Object> entity = Entity.entity(request, mediaType);
        Response r = client.target(restURL).request().put(entity, Response.class);
        client.close();
        return null;
    }

    /**
     * Prepared for multithreaded clients.
     *
     * @param restURL
     */
    public static void init(String restURL) {
        ClientConnectionManager cm = new ThreadSafeClientConnManager();
        HttpClient httpClient = new DefaultHttpClient(cm);
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
        ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
        root = client.target(restURL);
    }
}
